package cn.com.taiji.oospub.wechat.service.impl;

import cn.com.taiji.oospub.wechat.common.tool.TimeTools;
import cn.com.taiji.oospub.wechat.entity.SchoolBaseInfo;
import cn.com.taiji.oospub.wechat.entity.WeekMenu;
import cn.com.taiji.oospub.wechat.mapper.SchoolBaseInfoMapper;
import cn.com.taiji.oospub.wechat.mapper.WeekMenuMapper;
import cn.com.taiji.oospub.wechat.service.IWeekMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * <p>
 * 菜品表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-10-28
 */
@Service
public class WeekMenuServiceImpl extends ServiceImpl<WeekMenuMapper, WeekMenu> implements IWeekMenuService {

    @Autowired
    private WeekMenuMapper weekMenuMapper;
    @Autowired
    private SchoolBaseInfoMapper schoolBaseInfoMapper;

    @Override
    public void exportExcel(QueryWrapper<WeekMenu> weekMenuModel, HttpServletResponse response) {
        try {
            String fileName = ""; //文件名
            fileName = new String(("周菜单表.xls").getBytes("gbk"),"iso8859-1");
            OutputStream os = response.getOutputStream();
            response.reset();
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Content-disposition","attachment; filename="+fileName);

            //大标题字体
            WritableCellFormat wcfTitle = new WritableCellFormat();
            WritableFont BigTitleFont = new WritableFont(WritableFont.createFont("宋体"), 18, WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfTitle.setAlignment(jxl.format.Alignment.CENTRE);
            wcfTitle.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcfTitle.setFont(BigTitleFont);
            wcfTitle.setBorder(Border.ALL, BorderLineStyle.THIN);

            //表头字体
            WritableCellFormat wcfGrid = new WritableCellFormat();
            WritableFont gridTitleFont = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfGrid.setAlignment(jxl.format.Alignment.CENTRE);
            wcfGrid.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcfGrid.setFont(gridTitleFont);
            wcfGrid.setBorder(Border.ALL, BorderLineStyle.THIN);
            wcfGrid.setWrap(true);

            //内容字体
            WritableCellFormat wcfL = new WritableCellFormat();
            wcfL.setAlignment(jxl.format.Alignment.LEFT);
            wcfL.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcfL.setBorder(Border.ALL, BorderLineStyle.THIN);
            wcfL.setWrap(true);

            WritableWorkbook wb = Workbook.createWorkbook(os);
            WritableSheet sheet = wb.createSheet("周菜单表", 0);

            //大标题
            sheet.mergeCells(0, 0, 11, 0);
            sheet.setRowView(0, 1000);
            sheet.addCell(new Label(0, 0, "周菜单表",wcfTitle));
            //表头
            sheet.addCell(new Label(0, 1, "所属项目中文名称",wcfGrid));
            sheet.setColumnView(1, 20);
            sheet.addCell(new Label(1, 1, "所属项目英文名称",wcfGrid));
            sheet.setColumnView(2, 30);
            sheet.addCell(new Label(2, 1, "菜品中文名称",wcfGrid));
            sheet.setColumnView(3, 20);
            sheet.addCell(new Label(3, 1, "菜品英文名称",wcfGrid));
            sheet.setColumnView(4, 20);
            sheet.addCell(new Label(4, 1, "菜品编号",wcfGrid));
            sheet.setColumnView(5, 30);
            sheet.addCell(new Label(5, 1, "餐次",wcfGrid));
            sheet.setColumnView(6, 30);
            sheet.addCell(new Label(6, 1, "计划生产份数",wcfGrid));
            sheet.setColumnView(7, 30);
            sheet.addCell(new Label(7, 1, "剩余份数",wcfGrid));
            sheet.setColumnView(8, 30);
            sheet.addCell(new Label(8, 1, "售价",wcfGrid));
            sheet.setColumnView(9, 30);
            sheet.addCell(new Label(9, 1, "创建人",wcfGrid));
            sheet.setColumnView(10, 10);
            sheet.addCell(new Label(10, 1, "创建时间",wcfGrid));
            sheet.setColumnView(11, 10);
            sheet.addCell(new Label(11, 1, "描述",wcfGrid));
            sheet.setColumnView(12, 50);

            //根据条件查找
            List<WeekMenu> resultList = weekMenuMapper.selectList(weekMenuModel);

            int row = 2;
            int col = 0;
            if(null != resultList && resultList.size()>0){
                //商品分类细目
                for(int i=0;i<resultList.size();i++){
                    WeekMenu weekMenu = resultList.get(i);
                    SchoolBaseInfo school = schoolBaseInfoMapper.selectById(weekMenu.getSchoolId());
                    sheet.addCell(new Label(col++, row, school.getNameZh(),wcfL));//学校中文名
                    sheet.addCell(new Label(col++, row, school.getNameEn(),wcfL));//学校英文名
                    sheet.addCell(new Label(col++, row, weekMenu.getNameCh(),wcfL));//分类中文名
                    sheet.addCell(new Label(col++, row, weekMenu.getNameEn(),wcfL));//分类英文名
                    sheet.addCell(new Label(col++, row, weekMenu.getPosNo(),wcfL));//菜品编号
                    if("1".equals(weekMenu.getMealTimeId())){
                        sheet.addCell(new Label(col++, row, "早餐",wcfL));
                    }
                    if("2".equals(weekMenu.getMealTimeId())){
                        sheet.addCell(new Label(col++, row, "午餐",wcfL));
                    }
                    if("3".equals(weekMenu.getMealTimeId())){
                        sheet.addCell(new Label(col++, row, "晚餐",wcfL));
                    }
                    if(null != weekMenu.getPlanProductCount()){
                        sheet.addCell(new Label(col++, row, weekMenu.getPlanProductCount().toString(),wcfL));
                    }
                    if(null != weekMenu.getRemainCount()){
                        sheet.addCell(new Label(col++, row, weekMenu.getRemainCount().toString(),wcfL));
                    }
                    if(null != weekMenu.getPrice()){
                        sheet.addCell(new Label(col++, row, weekMenu.getPrice().toString(),wcfL));
                    }
                    if(null != weekMenu.getUserName()){
                        sheet.addCell(new Label(col++, row, weekMenu.getUserName().toString(),wcfL));
                    }

                    sheet.addCell(new Label(col++, row, TimeTools.calendar2String(weekMenu.getLastChanged(), "yyyy-MM-dd"),wcfL));//创建时间
                    sheet.addCell(new Label(col++, row, weekMenu.getDescription(),wcfL));//备注
                    row ++;
                    col = 0;
                }
            }
            wb.write();
            wb.close();
            os.flush();
            os.close();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public WeekMenu findById(String id) {
        return weekMenuMapper.selectById(id);
    }
}
